package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R save(@RequestBody Category category){
        //接收参数
        log.info("category:{}",category);
        //维护公共字段...（完成了）
        //新增数据
        boolean save = categoryService.save(category);
        return save?R.success("新增成功"):R.error("新增失败");
    }
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize){
        //1.接收参数
        log.info("page:{},pageSize:{}",page,pageSize);
        //2.构建分页条件
        IPage<Category> categoryPage = new Page<>(page,pageSize);
        //3.构建查询条件warpper
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //先按照sort字段排序   再按照更新时间倒序排序
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //4.分页查询
        categoryService.page(categoryPage,wrapper);
        //5.返回结果
        return R.success(categoryPage);
    }
    @PutMapping
    public R update(@RequestBody Category category){
        //1.接收参数
        log.info("修改的category为：{}",category);
//        //2.维护字段修改人，修改时间
//        category.setUpdateUser((Long) session.getAttribute("empId"));
//        category.setUpdateTime(LocalDateTime.now());
        //3.修改
        boolean b = categoryService.updateById(category);
        //4.返回结果
        return b?R.success("修改成功"):R.error("修改失败");
    }
    @DeleteMapping
    public R delete(Long id){
        //1.接收参数
        log.info("删除的id为：{}",id);
        //2.删除
//        categoryService.removeById(id);
        boolean msg = categoryService.remove(id);
        //3.返回结果
        return msg?R.success("删除成功"):R.error("删除失败");
    }
    /**
     * 根据条件查询分类数据
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //1.接收参数
        log.info("category:{}",category);
        //2.构建条件查询
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq( category.getType()!=null, Category::getType,category.getType());
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //3.查询
        List<Category> categoryList = categoryService.list(wrapper);
        //返回数据
        return R.success(categoryList);
    }
}
